package com.hao.user.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hao.common.code.ErrorCode;
import com.hao.common.execption.BusinessException;
import com.hao.common.response.BaseResponse;
import com.hao.common.utils.ResultUtils;
import com.hao.user.app.model.dto.address.AddAddressRequest;
import com.hao.user.app.model.dto.address.ListAddressRequest;
import com.hao.user.app.model.dto.address.UpdateAddressRequest;
import com.hao.user.app.model.entity.Address;
import com.hao.user.app.service.AddressService;
import io.netty.channel.unix.Errors;
import io.swagger.annotations.Api;
import jdk.internal.dynalink.linker.LinkerServices;
import org.redisson.MapWriterTask;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author haoge
 * @version 1.0
 * @date 2023/5/25 21:22
 */
@Api (tags = "用户地址管理")
@RestController
@RequestMapping ("/address")
public class AddressController {
    @Resource
    private AddressService addressService;

    @PostMapping ("/add")
    public BaseResponse<Boolean> addAddress(@RequestBody @Valid AddAddressRequest addAddressRequest) {
        if (addAddressRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = addressService.addAddress(addAddressRequest);
        if (!result) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(result);
    }

    @PostMapping ("/delete")
    public BaseResponse<Boolean> deleteAddress(@RequestParam Long id, @RequestParam Long userId) {
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (userId == null || userId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<Address> addressQueryWrapper = new QueryWrapper<>();
        addressQueryWrapper.eq("id", id);
        addressQueryWrapper.eq("userId", userId);

        boolean result = addressService.remove(addressQueryWrapper);

        if (!result) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }

        return ResultUtils.success(result);
    }

    @PostMapping ("/update")
    public BaseResponse<Boolean> updateAddressById(@RequestBody @Valid UpdateAddressRequest updateAddressRequest) {
        if (updateAddressRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = addressService.updateAddressById(updateAddressRequest);
        if (!result) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(result);
    }

    @GetMapping ("/get")
    public BaseResponse<List<Address>> getAddressByUser(Long userId) {
        if (userId == null || userId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<Address> addressQueryWrapper = new QueryWrapper<>();
        addressQueryWrapper.eq("userId", userId);

        List<Address> list = addressService.list(addressQueryWrapper);

        return ResultUtils.success(list);
    }

    @GetMapping ("/list")
    public BaseResponse<Page<Address>> listAddressByPage(ListAddressRequest listAddressRequest) {
        if (listAddressRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<Address> addressPage = addressService.listAddressByPage(listAddressRequest);
        return ResultUtils.success(addressPage);
    }
}
